﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    public partial class NameQuery : Form
    {
        public NameQuery()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;");
        private void NameQuery_Load(object sender, EventArgs e)
        {
            var sql = "select UserName,PassWord,RealName,IdentityCard,Sex,Birthday from UserInformation";
            SqlCommand comm = new SqlCommand(sql, conn);
            //创建数据适配器
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            //创建数据集
            DataSet ds = new DataSet();
            //向数据集填充数据
            adp.Fill(ds);
            //是否允许自动创建列
            this.dgv_NameQuery.AutoGenerateColumns = false;
            //不允许出现空行
            this.dgv_NameQuery.AllowUserToAddRows = false;
            //整行选中
            this.dgv_NameQuery.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //是否允许多选
            this.dgv_NameQuery.MultiSelect = false;
            this.dgv_NameQuery.RowTemplate.Height = 50;
        }

        /// <summary>
        /// 搜索
        /// </summary>
        SIMS_GUI_Item_DataBase db = new SIMS_GUI_Item_DataBase();
        private void btn_Search_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select a.UserName,a.PassWord,a.RealName,a.IdentityCard,b.GradeName,a.Sex,a.Birthday from UserInformation a
                        inner join Grade b on a.GradeId=b.GradeId where 1=1");//联合查询语句
            if (!string.IsNullOrWhiteSpace(this.txt_RealName.Text))
                sb.AppendFormat(" and a.RealName like '%{0}%'",this.txt_RealName.Text.Trim());
            if (!string.IsNullOrWhiteSpace(this.txt_UserName.Text))
                sb.AppendFormat(" and a.UserName like '%{0}%'", this.txt_UserName.Text.Trim());
            SqlDataAdapter adp = new SqlDataAdapter(sb.ToString(),conn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            this.dgv_NameQuery.DataSource=ds.Tables[0];
        }

        /// <summary>
        /// 修改个人信息点击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ModificationPersonalInformation_Click(object sender, EventArgs e)
        {
            AddNewStudent ans = new AddNewStudent();
            ans.BackColor = Color.FromArgb(50, 69, 101);
            ans.Width = 1192;
            ans.Height = 720;
            ans.Location = new Point(209, 200);
            ans.Show();
        }

        /// <summary>
        /// 添加成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AddScore_Click(object sender, EventArgs e)
        {
            AddAchievement ad = new AddAchievement();
            ad.Show();
        }

        /// <summary>
        /// 修改个人信息按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModificationPersonalInformation_Click(object sender, EventArgs e)
        {
            //获取参数
            string RealName = this.dgv_NameQuery.CurrentRow.Cells[0].Value.ToString();
            string GradeName = this.dgv_NameQuery.CurrentRow.Cells[1].Value.ToString();
            string UserName = this.dgv_NameQuery.CurrentRow.Cells[2].Value.ToString();
            string PassWord = this.dgv_NameQuery.CurrentRow.Cells[3].Value.ToString();
            string IdentityCard = this.dgv_NameQuery.CurrentRow.Cells[4].Value.ToString();
            string Sex = this.dgv_NameQuery.CurrentRow.Cells[5].Value.ToString();
            var Birthday = Convert.ToDateTime(this.dgv_NameQuery.CurrentRow.Cells[6].Value.ToString());
            ModificationPersonalInformation mpi = new ModificationPersonalInformation(RealName,GradeName,UserName,PassWord,IdentityCard,Sex,Birthday);
            mpi.BackColor = Color.FromArgb(50, 69, 101);
            mpi.Width = 1215;
            mpi.Height = 850;
            mpi.Location = new Point(209, 175);
            mpi.Show();
        }

        private void AddAchievement_Click(object sender, EventArgs e)
        {
            string RealName= Convert.ToString(this.dgv_NameQuery.CurrentRow.Cells[0].Value);
            string UserName = Convert.ToString(this.dgv_NameQuery.CurrentRow.Cells[2].Value);
            AddAchievement ad = new AddAchievement(RealName,UserName);
            ad.BackColor = Color.FromArgb(50, 69, 101);
            ad.Width = 1215;
            ad.Height = 850;
            ad.Location = new Point(209, 175);
            ad.Show();
        }

        private void DeleteAchievement_Click(object sender, EventArgs e)
        {
            string UserName = this.dgv_NameQuery.CurrentRow.Cells[2].Value.ToString();
            string sql = string.Format("delete UserInformation where UserName='{0}'", UserName);
            int number = Convert.ToInt32(db.ExecNonQuery(sql));
            if (number > 0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }
    }
}
